// linesdetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    update: 0,
    downDate: 0,
    server: false,
    appColor: app.appColor
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.IsScanCOde = false;
    app.showLoadings();
    app.line.id = options.id;
  },

  selectDate: function() {
    wx.navigateTo({
      url: '../../../pages/calendar/calendar'
    });
  },

  // 后一天数据
  downDay: function(e) {
    if (this.data.downDay == 'null') {
      return;
    }
    app.startdate = this.data.downDay;
    app.showLoadings();
    this.setData({
      date: this.data.downDay
    })
    this.load(this.data.date);
  },

  // 前一天数据
  upDay: function() {
    if (this.data.upDay == 'null') {
      return;
    }
    app.startdate = this.data.upDay;
    app.showLoadings();
    this.setData({
      date: this.data.upDay
    })
    this.load(this.data.date);
  },

  // CardID提示框
  hints: function() {
    wx.showModal({
      content: '该班次已发车或已经售完，请选择其它班次',
      showCancel: false,
      success: function(res) {
        if (res.confirm) {}
      }
    });
  },
  load: function(date) {
    if (date == '') {
      date = app.startdate
    }
    var that = this;

    wx.request({
      url: app.serviceurl + 'CustomLine/CustomQueryBus/',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        BusLineID: app.line.id,
        StartDate: date
      },
      dataType: 'json',
      success: function(res) {
        if (res.data.BeforeDate == 'null') {
          that.setData({
            leftOff: true,
            colors: '#dedede'
          })
        } else {
          that.setData({
            colors: '#fff',
            leftOff: false
          })
        };

        if (res.data.NextDate == 'null') {
          that.setData({
            rightOff: true,
            rightColors: '#dedede'
          })
        } else {
          that.setData({
            rightColors: '#fff',
            rightOff: false
          })
        };

        if (!res.data.Result) {
          app.hideLoadings();
          that.setData({
            data: '',
            upDay: res.data.BeforeDate,
            downDay: res.data.NextDate,
            show: res.data
          })
          return;
        }

        if (res.data.BusPlans !== null && typeof(res.data.BusPlans) !== undefined) {
          res.data.BusPlans.forEach(function(e) {
            if (e.IsEnable == false) {
              e.color = true;
            } else {
              e.color = false;
            }
          })
        }
        var Points = res.data.TakePoints;
        var obj = {};
        obj.getOn = [],
          obj.getOnID = [],
          obj.getOff = [],
          obj.getOffID = [],
          obj.onLocation = [],
          obj.offLocation = [];
        if (Points !== null && typeof(Points) !== undefined) {
          Points.forEach(function(e) {
            var locat = e.Coordinate.split(',');
            if (e.IsUpPoint == true) {
              obj.getOn.push(e.PointName);
              obj.getOnID.push(e.PointID);
              obj.onLocation.push(e.Coordinate);
            } else {
              obj.getOff.push(e.PointName);
              obj.getOffID.push(e.PointID);
              obj.offLocation.push(e.Coordinate);
            }
          })
        }

        that.setData({
          data: res.data.BusPlans,
          getOn: obj.getOn,
          getOff: obj.getOff,
          upDay: res.data.BeforeDate,
          downDay: res.data.NextDate,
          show: res.data
        })

        wx.setStorage({
          key: 'getOn',
          data: obj
        });

        app.hideLoadings();
      },
      fail: function(err) {
        app.hideLoadings();
      }

    })
  },


  // 查看车辆实时位置
  positionMessage: function() {
    wx.navigateTo({
      url: '../realTimeMessage/realTimeMessage',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.load(app.startdate);
    let isDate = app.startdate;
    if (isDate == null) {
      this.setData({
        date: app.dates()
      })
    } else {
      this.setData({
        date: isDate
      });
    }

    // 未付款订单提示
    if (app.hint == true) {
      wx.showModal({
        title: "有未完成订单",
        content: "请在未完成订单中处理该订单，5分钟内无法重复预定！",
        showCancel: false,
        confirmText: "确定",
        complete: function(res) {
          app.hint = false;
          wx.navigateTo({
            url: '../detail/detail',
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },

  pre: function(e) {
    if (app.IsRegister == true) {
      var starttime = e.currentTarget.dataset.starttime,
        surplus = e.currentTarget.dataset.surplus,
        price = e.currentTarget.dataset.price,
        startdate = e.currentTarget.dataset.startdate + e.currentTarget.dataset.starttime,
        isenable = e.currentTarget.dataset.isenable,
        busid = e.currentTarget.dataset.coustomid,
        index = e.currentTarget.dataset.index
      if (isenable == false) {
        this.hints();
        return
      }
      app.line.BusPlanId = e.currentTarget.dataset.coustomid;
      var datas = this.data.data[index].ActivityDiscounts;
      wx.setStorage({
        key: "activitys",
        data: datas
      })
      wx.navigateTo({
        url: '../linepre/linepre?starttime=' + starttime + '&price=' + price + '&date=' + this.data.date + '&surplus=' + surplus + '&busid=' + busid,
      })
    } else {
      wx.showModal({
        content: '请绑定手机号后再来购票哦！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../../pages/bindPhone/bindPhone',
            });
          }
        }
      });
    }
  },

})